package com.example.anew.journey.mynewjourney.logic.login;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.base.BaseRegisteredAndLoginActivity;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.FirstChangeMainActivity;
import com.example.anew.journey.mynewjourney.logic.PasswordFreeLogin.PasswordFreeLoginActivity;
import com.example.anew.journey.mynewjourney.logic.forgotPassword.ForgotPasswordActivity;
import com.example.anew.journey.mynewjourney.logic.login.persenter.LoginPersenter;
import com.example.anew.journey.mynewjourney.logic.login.view.ISLoginView;
import com.example.anew.journey.mynewjourney.logic.registeredOrLogin.RegisteredOrLoginActivity;
import com.example.anew.journey.mynewjourney.logic.resetPassword.bean.ResetPhoneAndNumber;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.jesse.nativelogger.NLogger;

/**
 * 杭州惟君医疗科技有限公司15
 * Created by 20433 on 2018/3/2
 * @desc 登陆
 */

public class LoginActivity extends BaseRegisteredAndLoginActivity implements View.OnClickListener,ISLoginView {

    private ProgressBar progressBar;
    private TextView tv_password_login;
    private EditText tv_et_phone;
    private EditText et_password;
    private Button bt_login;
    private TextView tv_forgotpassword;
    private ImageView iv_wechat;
    private ImageView iv_qq;
    private LoginPersenter mLoginPersenter = new LoginPersenter(this);

    /**
     * @desc 初始化数据 onCreate()
     */
    @Override
    protected void initView(View view) {
        // todo Toolbar的一系列操作
        setToolbarBg(0xffffffff);
        displayBtRelease();
        setBtReleaseBg(0XFFFFFFFF);
        setBtRelease("免密码登录",18,0xff666666);
        hiddenReturn();

        EventBus.getDefault().post(this);
        findLoginView(view);
    }
    /**
     * @desc 点击发布按钮
     */
    protected void onClickRelease() {
        Intent intent = new Intent(LoginActivity.this, RegisteredOrLoginActivity.class); //PasswordFreeLoginActivity
        //intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        intent.putExtra("my_page_integration_code",1);
        startActivity(intent);

    }
    /**
     * @desc 主要内容的布局资源填充进来
     */
    @Override
    protected View setMainContentView() {
        return View.inflate(this, R.layout.activity_login, null);
    }
    /**============方法区====================
     * @param view*/
    private void findLoginView(View view) {
        progressBar = (ProgressBar) findViewById(R.id.activity_login_progressBar);
        tv_password_login = (TextView) findViewById(R.id.activity_login_tv_password_login);
        tv_et_phone = (EditText) findViewById(R.id.activity_login_tv_et_phone);
        et_password = (EditText) findViewById(R.id.activity_login_tv_et_password);

        bt_login = (Button) findViewById(R.id.activity_login_tv_bt_login);
        tv_forgotpassword = (TextView) findViewById(R.id.activity_login_tv_forgotpassword);
        iv_wechat = (ImageView) findViewById(R.id.activity_login_tv_iv_wechat);
        iv_qq = (ImageView) findViewById(R.id.activity_login_tv_iv_qq);

        bt_login.setOnClickListener(this);
        tv_forgotpassword.setOnClickListener(this);
        iv_wechat.setOnClickListener(this);
        iv_qq.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.activity_login_tv_bt_login:
                mLoginPersenter.onClickLogin(this);
                break;
            case R.id.activity_login_tv_forgotpassword:
                mLoginPersenter.onClickForgotPassword();
                break;
            case R.id.activity_login_tv_iv_wechat:
                mLoginPersenter.onClickWecht(this);
                break;
            case R.id.activity_login_tv_iv_qq:
                mLoginPersenter.onClickQQ(this);
                break;
        }
    }
    /**
     * @desc Activity 销毁 释放资源
     */
    @Override
    protected void releaseResources() {
        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }

    }
    // 登陆成功跳转到主页
    @Override
    public void toFirstChangeMainActivity() {
        Intent intent_01 = new Intent(LoginActivity.this, FirstChangeMainActivity.class);
        intent_01.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent_01);
    }
    // 登陆失败显示提示

    @Override
    public void showToastInformation(String str) {
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }
    // 获取手机号
    @Override
    public String getPhoneNumber() {
        String phonenNumber = tv_et_phone.getText().toString().trim();
       // String regex = "^((13[0-9])|(14[0-9])|(15([0-9]))|(17[0-9])|(18[0-9]))\\d{8}$";
        String regex = "^((1[0-9][0-9]))\\d{8}$";
        if (phonenNumber.length() != 11) {
            // System.out.println("手机号应为11位数");
            Toast.makeText(this,"手机号应为11位数",Toast.LENGTH_SHORT).show();
            return  null;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phonenNumber);
            boolean isMatch = m.matches();
            if (isMatch) {
                return phonenNumber;
            } else {
                // System.out.println("您的手机号" + phoneNumber + "格式错误");
                Toast.makeText(this,"手机号格式错误",Toast.LENGTH_SHORT).show();
                return  null;
            }
        }

    }
    // 获取密码
    @Override
    public String getPassword() {
        return et_password.getText().toString().trim();
    }
    //跳转到忘记密码
    @Override
    public void toForgotPassword() {
        Intent intent = new Intent(LoginActivity.this,ForgotPasswordActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        startActivity(intent);
    }

    //跳转到免密码登陆
    @Override
    public void toPasswordFreeLogin() {
        Intent intent = new Intent(LoginActivity.this,PasswordFreeLoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        startActivity(intent);
    }
    // 显示进度
    @Override
    public void displayProgress() {

    }
    // 隐藏进度
    @Override
    public void hiddenProgress() {

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessage(ResetPhoneAndNumber event) {
        tv_et_phone.setText(event.getPhoneNumber());
        et_password.setText(event.getPassword());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        NLogger.e("TAG","=========222=fgcj===========");
    }
}
